#include "coefficients.h"

using namespace std;

coefficients::coefficients(const char* setting_file_name)
	:test_times(20),
	time_limit(1.0),
	II(true),
	SA(true),
	TS(true),
	II_neighbor_per_time(99),
	II_retry_limit(9),
	SA_start_tempture(1000.0),
	SA_init_cool_rate(0.33),
	SA_final_temperature(1.0),
	SA_iterate_length(15),
	TS_neighbor_per_time(100),
	TS_retry_limit(1),
	TS_tubu_list_len(100)
{
	char temp_string[999] = {0};
	FILE* setting_file = fopen(setting_file_name , "r");
	//--
	input_table_["test_times"] = new entry_int(&test_times);
	input_table_["time_limit"] = new entry_double(&time_limit);

	input_table_["II"] = new entry_switch(&II);
	input_table_["SA"] = new entry_switch(&SA);
	input_table_["TS"] = new entry_switch(&TS);

	input_table_["II_neighbor_per_time"] = new entry_int(&II_neighbor_per_time);
	input_table_["II_retry_limit"] = new entry_int(&II_retry_limit);

	input_table_["SA_start_tempture"] = new entry_double(&SA_start_tempture);
	input_table_["SA_init_cool_rate"] = new entry_double(&SA_init_cool_rate);
	input_table_["SA_final_temperature"] = new entry_double(&SA_final_temperature);
	input_table_["SA_iterate_length"] = new entry_int(&SA_iterate_length);

	input_table_["TS_neighbor_per_time"] = new entry_int(&TS_neighbor_per_time);
	input_table_["TS_retry_limit"] = new entry_int(&TS_retry_limit);
	input_table_["TS_tubu_list_len"] = new entry_size_t(&TS_tubu_list_len);

	if(!setting_file) {
		fprintf(stderr, "Can not open setting file\n");
	}
	else {
		fscanf(setting_file , "%s" , temp_string);
		while(strcmp(temp_string , "#END") != 0) {
			input_table_[temp_string]->read_from_file(setting_file);
			fscanf(setting_file , "%s" , temp_string);
		}
	}
}

string coefficients::to_string() {
	stringstream temp_stringstream;
	for (auto& entry : input_table_) {
		temp_stringstream << entry.first << "," << entry.second->to_string() << endl;
	}
	return temp_stringstream.str();
}